package com.un.ebs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.product.domain.ProductClassAttrGroup;
import com.un.ebs.product.mapper.ProductClassAttrGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.un.ebs.product.service.ProductClassAttrGroupService;
import com.un.ebs.product.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-11-03
 */
@RestController
@RequestMapping("/productClassAttrGroup")
@Api(value = "product-class-attr-group", description = "品类属性分组")
public class ProductClassAttrGroupController {

    @Autowired
    private ProductClassAttrGroupService service;

    @Autowired
    private ProductClassAttrGroupMapper productClassAttrGroupMapper;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody ProductClassAttrGroupAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody ProductClassAttrGroupUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 ProductClassAttrGroup")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list/{classId}")
    @ApiOperation(value = "list", notes = "")
    public WebResult list(@PathVariable String classId){
        LambdaQueryWrapper<ProductClassAttrGroup> lq = Wrappers.lambdaQuery();
        lq.eq(ProductClassAttrGroup::getClassId,classId);
        lq.orderByAsc(ProductClassAttrGroup::getSort);
        return WebUtils.success(productClassAttrGroupMapper.selectList(lq));
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 ProductClassAttrGroup 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 ProductClassAttrGroup")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

}